package com.startest.sms.web;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.startest.sms.utils.PhoneRandomCode;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/sms")
public class SMSController {

    /**
     * 阿里短信sms
     * @param phoneNum 手机号
     * @return
     */
    @RequestMapping(value = "/sendSMS",method = RequestMethod.POST)
    public String sendSMS(@RequestParam("phoneNum") String phoneNum){
        String RegionID = "cn-hangzhou";
        String AccessKeyID = "LTAIUKbEzrEr2C9E";
        String AccessKeySecret = "YU7dZ5PXmzb0HGD71r9sx7vHMefOPv";
        DefaultProfile profile = DefaultProfile.getProfile(RegionID, AccessKeyID, AccessKeySecret);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("PhoneNumbers", phoneNum);
        request.putQueryParameter("SignName", "星天海洋");
        request.putQueryParameter("TemplateCode", "SMS_160856381");
        String code = PhoneRandomCode.randomCode();
        request.putQueryParameter("TemplateParam", "{\"code\":"+code+"}");
        try {
            CommonResponse  response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getData();
        } catch (Exception e) {
            e.printStackTrace();
            return "{\"Code\":\"fail\"}}";
        }
    }
}
